package com.ruoyi.web.controller.orders_tools;


import java.util.List;
import java.util.Objects;

import com.ruoyi.system.domain.SysOrders;
import com.ruoyi.system.service.ISysOrdersService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysOrdersTools;
import com.ruoyi.system.service.ISysOrdersToolsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 协调工具Controller
 *
 * @author ruoyi
 * @date 2021-11-18
 */
@RestController
@RequestMapping("/system/tools_orders")
public class SysOrdersToolsController extends BaseController
{
    @Autowired
    private ISysOrdersToolsService sysOrdersToolsService;

    @Autowired
    private ISysOrdersService sysOrdersService;

    /**
     * 查询协调工具列表
     */
    @PreAuthorize("@ss.hasPermi('system:tools_orders:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysOrdersTools sysOrdersTools)
    {
        startPage();
        List<SysOrdersTools> list = sysOrdersToolsService.selectSysOrdersToolsList(sysOrdersTools);
        return getDataTable(list);
    }

    /**
     * 导出协调工具列表
     */
    @PreAuthorize("@ss.hasPermi('system:tools_orders:export')")
    @Log(title = "协调工具", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysOrdersTools sysOrdersTools)
    {
        List<SysOrdersTools> list = sysOrdersToolsService.selectSysOrdersToolsList(sysOrdersTools);
        ExcelUtil<SysOrdersTools> util = new ExcelUtil<SysOrdersTools>(SysOrdersTools.class);
        return util.exportExcel(list, "协调工具数据");
    }

    /**
     * 获取协调工具详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:tools_orders:query')")
    @GetMapping(value = "/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") String orderId)
    {
        return AjaxResult.success(sysOrdersToolsService.selectSysOrdersToolsByOrderId(orderId));
    }

    /**
     * 新增协调工具
     */
    @PreAuthorize("@ss.hasPermi('system:tools_orders:add')")
    @Log(title = "协调工具", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysOrdersTools sysOrdersTools)
    {
        return toAjax(sysOrdersToolsService.insertSysOrdersTools(sysOrdersTools));
    }

    /**
     * 修改协调工具
     */
    @PreAuthorize("@ss.hasPermi('system:tools_orders:edit')")
    @Log(title = "协调工具", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysOrdersTools sysOrdersTools)

    {
//        Objects.requireNonNull(sysOrdersTools.getOrderPrice(),"价格为空");
        //        将订单状态改为
        SysOrders sysOrders =sysOrdersService.selectSysOrdersByOrderId(sysOrdersTools.getOrderId());
        sysOrders.setOrderStatus(sysOrdersTools.getOrderStatus());
        sysOrdersService.updateSysOrders(sysOrders);
        sysOrdersTools.setOrderCoordinate(getUsername());

        return toAjax(sysOrdersToolsService.updateSysOrdersTools(sysOrdersTools));
    }

    /**
     * 删除协调工具
     */
    @PreAuthorize("@ss.hasPermi('system:tools_orders:remove')")
    @Log(title = "协调工具", businessType = BusinessType.DELETE)
    @DeleteMapping("/{orderIds}")
    public AjaxResult remove(@PathVariable String[] orderIds)
    {
        return toAjax(sysOrdersToolsService.deleteSysOrdersToolsByOrderIds(orderIds));
    }

    //申请工具

    @PostMapping("/add")
    public String addOrders_tools(@RequestBody SysOrdersTools sysOrdersTools){

//        将订单状态改为待维修
        SysOrders sysOrders =sysOrdersService.selectSysOrdersByOrderId(sysOrdersTools.getOrderId());
        sysOrders.setOrderStatus("未协调");
        sysOrdersService.updateSysOrders(sysOrders);

        //申请工具表
        sysOrdersTools.setOrderStatus("未协调");
        sysOrdersToolsService.insertSysOrdersTools(sysOrdersTools);

        return "success";

    }

//    @PostMapping("/myorders/detail")
//    @ResponseBody
//    public SysOrdersTools getMyorders_detail(@RequestBody SysOrders sysOrders){
//
//        return  sysOrdersToolsService.selectSysOrdersToolsByOrderId(sysOrders.getOrderId());
//
//    }

}